
from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

# 初始化DuckDB内存数据库
engine = create_engine("duckdb:///:memory:")
Base = declarative_base()

# 定义数据模型
class User(Base):
    __tablename__ = "users"
    id = Column(Integer, primary_key=True)
    name = Column(String)
    email = Column(String)

# 创建表结构
Base.metadata.create_all(engine)

# 创建会话
Session = sessionmaker(bind=engine)
session = Session()

# 插入数据
session.add_all([
    User(name="Alice", email="alice@example.com"),
    User(name="Bob", email="bob@example.com")
])
session.commit()

# 查询数据
users = session.query(User).filter(User.name.like("%A%")).all()
for user in users:
    print(f"ID: {user.id}, Name: {user.name}")

# 更新数据
session.query(User).filter(User.name == "Bob").update({"email": "bob_new@example.com"})
session.commit()

# 删除数据
session.query(User).filter(User.name == "Alice").delete()
session.commit()
